package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.Salutation;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.bo.Territory;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminAgentRegBean extends AbstractBean {

	private AgentService service;
	private MasterSetupService setupService;
	public static final String NAV_REG_AGENT="adminCreateAgent";
	public static final String BEAN_NAME="adminAgentRegBean";
	public static final String NAV_CREATE_AGENT_SUCCESS="adminCreateAgentAck";
	private Agent agent;
	private String compCountryId;
	private String compStateId;
	private String mailCountryId;
	private String mailStateId;
	private String compTerrId;
	private List<SelectItem> countryList;
	private List<SelectItem> compStateList;
	private List<SelectItem> mailStateList;
	private List<SelectItem> mailSelectList;
	private List<SelectItem> compTerritoryList;
	private String mailBy = CommonConstant.MAIL_SELECT_OTHER;
	private boolean checkFlag = true;
	private String regNo;
	private String login;
	private String password;

	private List<SelectItem> salutationSelectList;
	
	public List<SelectItem> getSalutationSelectList() {
		try{
			List<Salutation> sList = setupService.getSalutationList();
			salutationSelectList= new ArrayList<SelectItem>();
			for(Salutation s:sList){
				SelectItem item = new SelectItem(s.getSalutationId(),s.getDesc());
				salutationSelectList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return salutationSelectList;
	}
	
	public void setService(AgentService service) {
		this.service = service;
	}
	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}

	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public String getCompCountryId() {
		return compCountryId;
	}
	public void setCompCountryId(String compCountryId) {
		this.compCountryId = compCountryId;
	}
	public String getCompStateId() {
		return compStateId;
	}
	public void setCompStateId(String compStateId) {
		this.compStateId = compStateId;
	}
	public String getMailCountryId() {
		return mailCountryId;
	}
	public void setMailCountryId(String mailCountryId) {
		this.mailCountryId = mailCountryId;
	}
	public String getMailStateId() {
		return mailStateId;
	}
	public void setMailStateId(String mailStateId) {
		this.mailStateId = mailStateId;
	}
	public void setCompStateList(List<SelectItem> compStateList) {
		this.compStateList = compStateList;
	}
	public String getMailBy() {
		return mailBy;
	}
	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public String getRegNo() {
		return regNo;
	}
	public void setRegNo(String regNo) {
		this.regNo = regNo;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCompTerrId() {
		return compTerrId;
	}
	public void setCompTerrId(String compTerrId) {
		this.compTerrId = compTerrId;
	}
	/*-------------------------------------------------------------*/
	public String navRegAgent(){
		agent = new Agent();
		return NAV_REG_AGENT.concat("_"+getRoleName());
	}
	public List<SelectItem> getCountryList() {
		try{
			countryList = new ArrayList<SelectItem>();
			List<Country> cList = setupService.getCountryList();
			for(Country country:cList){
				//SelectItem item = new SelectItem(country.getCountryId(),country.getCode()+" "+country.getDesc());
				SelectItem item = new SelectItem(country.getCountryId(),country.getCode());
				countryList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countryList;
	}
	public List<SelectItem> getMailStateList() {
		try{
			mailStateList = new ArrayList<SelectItem>();
			if(mailCountryId!=null&&mailCountryId.length()>0){
				List<State> cList = setupService.getStateList(mailCountryId);
				for(State state:cList){
					//SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					SelectItem item = new SelectItem(state.getStateId(),state.getCode());
					mailStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return mailStateList;
	}
	public List<SelectItem> getCompStateList() {
		try{
			compStateList = new ArrayList<SelectItem>();
			if(compCountryId!=null&&compCountryId.length()>0){
				List<State> cList = setupService.getStateList(compCountryId);
				for(State state:cList){
					//SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					SelectItem item = new SelectItem(state.getStateId(),state.getCode());
					compStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return compStateList;
	}
	public List<SelectItem> getMailSelectList() {
		mailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_COMP_OTHER_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			mailSelectList.add(item);
		}

		return mailSelectList;
	}

	public void setMailBy(String mailBy) {
		this.mailBy = mailBy;
		if(mailBy.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
			checkFlag = false;
		}else{
			checkFlag = true;
		}
	}
	public List<SelectItem> getCompTerritoryList() {
		try{
			compTerritoryList = new ArrayList<SelectItem>();
			if(compCountryId!=null&&compCountryId.length()>0){
				List<Territory> cList = setupService.getTerritoryList(compStateId);
				for(Territory territory:cList){
					//SelectItem item = new SelectItem(territory.getTerritoryId(),territory.getCode()+" "+territory.getDesc());
					SelectItem item = new SelectItem(territory.getTerritoryId(),territory.getCode());
					compTerritoryList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return compTerritoryList;
	}

	public String createAgent(){
		try{
			if (this.agent.getSalutationId().equalsIgnoreCase(""))
				this.agent.setSalutationId(null);
			agent.setCompCountry(new Country(compCountryId));
			agent.setCompState(new State(compStateId));
			agent.setMailTerritory(new Territory(compTerrId));
			if(!checkFlag){
				agent.setMailAddress1(agent.getCompAddress1());
				agent.setMailAddress2(agent.getCompAddress2());
				agent.setMailCity(agent.getCompCity());
				agent.setMailPostcode(agent.getCompPostcode());
				agent.setMailCountry(agent.getCompCountry());
				agent.setMailState(agent.getCompState());
			}else{
				agent.setMailCountry(new Country(mailCountryId));
				agent.setMailState(new State(mailStateId));
			}

			regNo = service.agentRegistration(agent, false, getThisUser());
			login = agent.getEmail();
			password = SystemConstant.DEFAULT_PASSWORD;
			
			
			return NAV_CREATE_AGENT_SUCCESS.concat("_"+getRoleName());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return null;
	}
	
	public void mailTypeChangeListener(ActionEvent event){
		//UIInput uiInput = (UIInput) event.getComponent().findComponent("CompanyAdress1");
		//if (checkFlag){
		//	String add1 =uiInput.getValue().toString();
		//	this.agent.setMailAddress1(add1);
		//}
		
	}
}
